package cn.lls.controller.employee;

import cn.lls.base.result.R;
import cn.lls.base.util.MD5Util;
import cn.lls.entity.company.EmployeeAccountEntity;
import cn.lls.req.emp.EMPAccountReq;
import cn.lls.service.IEmployeeAccountService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 员工账号密码控制器，主要用于员工自己更改密码
 *
 * @ClassName EmployeePwdController
 * @Author 划水艺术家
 * @Date 2022/11/22 14:24
 * @Version 1.0
 **/

@Slf4j
@RequestMapping("/emp/pwd")
@RestController
@Api(tags = "员工账号密码控制器")
public class EmployeePwdController {

    @Autowired
    private IEmployeeAccountService employeeAccountService;

    @PutMapping
    @ApiOperation(value = "员工自己更改密码", notes = "员工自己更改密码", response = R.class, httpMethod = "PUT")
    public R empPutPwd(@RequestBody @Validated EMPAccountReq empAccountReq,
                      BindingResult bindingResult){
        LambdaUpdateWrapper<EmployeeAccountEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(EmployeeAccountEntity::getUserId, empAccountReq.getUserId());
        EmployeeAccountEntity employeeAccountEntity = new EmployeeAccountEntity();
        BeanUtils.copyProperties(empAccountReq, employeeAccountEntity);
        employeeAccountEntity.setLoginPassword(MD5Util.MD5Encode(empAccountReq.getLoginPassword()));
        log.info("empEntity = {}", employeeAccountEntity);
        employeeAccountService.update(employeeAccountEntity, updateWrapper);
        return R.success();
    }
}
